package com.rfid.afd.activity.demo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.EditText;
import android.widget.TextView;

import com.rfid.db.bean.Machine;
import com.rfid.rls_psw.R;
import com.rfid.util.DBManager;
import com.rfid.util.Util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchActivity extends AppCompatActivity implements TextWatcher {

    @BindView(R.id.edit)
    EditText mEditText;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;

    private List<Machine> mNotes;
    protected Map<String,Integer> mBarIndex;
    protected List<Machine> mData;
    private SearchActivity.MyAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);

        mEditText.addTextChangedListener(this);

        mNotes = DBManager.queryMachines();
        mBarIndex = new HashMap<>();
        for(int i=0;i<mNotes.size();i++) {
            mBarIndex.put(mNotes.get(i).getEpc(),i);
        }
        mData = new ArrayList<>();
        mData.addAll(mNotes);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        myAdapter = new SearchActivity.MyAdapter(mData);
        mRecyclerView.setAdapter(myAdapter);
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mRecyclerView.setLayoutAnimation(controller);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        System.out.println("s = " + s  + ",before = " + before + ",count = " + count);
    }

    @Override
    public void afterTextChanged(Editable s) {
        String keyword = s.toString();
        if(!TextUtils.isEmpty(keyword)) {
            Util.runOnAsyncTask(new Runnable() {
                @Override
                public void run() {
                    List<Machine>  data =DBManager.queryMachinesByDimInfo(keyword);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mData.clear();
                            mData.addAll(data);
                            myAdapter.notifyDataSetChanged();
                        }
                    });
                }
            },null);
        } else {
            mData.clear();
            mData.addAll(mNotes);
            myAdapter.notifyDataSetChanged();
        }
    }

    public class MyAdapter extends RecyclerView.Adapter<SearchActivity.MyAdapter.MyViewHolder> {

        private List<Machine> mRfids;

        public MyAdapter(List<Machine> data) {
            mRfids = data;
        }

        @NonNull
        @Override
        public SearchActivity.MyAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new SearchActivity.MyAdapter.MyViewHolder(getLayoutInflater().inflate(R.layout.check_machine_item,parent,false));
        }

        @Override
        public void onBindViewHolder(@NonNull SearchActivity.MyAdapter.MyViewHolder holder, int position) {
            Machine machine = mRfids.get(position);
            holder.name.setText(machine.getInfo());
            holder.barcode.setText(machine.getEpc());
            String reader = machine.getIdCard();
            if(TextUtils.isEmpty(reader)) {
                holder.idcard.setText("");
                holder.itemView.setBackgroundColor(Color.WHITE);
            } else {
                holder.idcard.setText(reader);
                holder.itemView.setBackgroundColor(Color.YELLOW);
            }
        }

        @Override
        public int getItemCount() {
            return mRfids.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.book_name)
            TextView name;
            @BindView(R.id.barcode)
            TextView barcode;
            @BindView(R.id.idcard)
            TextView idcard;
            public MyViewHolder(@NonNull View itemView) {
                super(itemView);
                ButterKnife.bind(this,itemView);
            }
        }
    }
}